
import 'dart:developer';

import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:guochaoapp/common/constant/api.dart';
import 'package:guochaoapp/common/utils/http_client.dart';
import 'package:guochaoapp/controller/mine/invoice_controller.dart';
import 'package:guochaoapp/model/invoice_entity.dart';
import 'package:guochaoapp/services/user_service.dart';

class GCAddInvoiceController extends GetxController {
  UserService userService = Get.find();

  var isEdit = false.obs;

  var textEdittingControllers = [
    TextEditingController(),
    TextEditingController(),
    TextEditingController(),
    TextEditingController(),
    TextEditingController(),
    TextEditingController(),
    TextEditingController(),
    TextEditingController(),
    TextEditingController(),
  ].obs;

  var titles = [
    "单位名称",
    "纳税人识别码",
    "注册地址",
    "注册电话",
    "开户银行",
    "银行账号",
    "收票姓名",
    "收票电话",
    "收票地址"
  ];

  submitTap() {

    for (var value in textEdittingControllers) {
      if(value.text.length == 0) {
        EasyLoading.showError("请确认信息填写完整");
        return;
      }

    }

    if(isEdit.value) {
      HttpClient.request(
          url: UserInvoiceApi.usersInvoice,
          method: Method.PUT,
          data: {
            "companyName": textEdittingControllers[0].text,
            "taxpayerIdNum": textEdittingControllers[1].text,
            "registerAddr": textEdittingControllers[2].text,
            "registerPhone": textEdittingControllers[3].text,
            "branchName": textEdittingControllers[4].text,
            "cardNo": textEdittingControllers[5].text,
            "receiverName": textEdittingControllers[6].text,
            "receiverPhone": textEdittingControllers[7].text,
            "receiverAddr": textEdittingControllers[8].text,
            "uid" : userService.userInfo.uid
          },
          onSuccess: (r, _) {
            GCInvoiceController i_controller = Get.find();
            i_controller.requestInvoiceInfo();
            Get.back();
          },
          onFailed: (err) {
            EasyLoading.showError(err);
          }
      );
    } else {
      HttpClient.request(
          url: UserInvoiceApi.usersInvoice,
          method: Method.POST,
          data: {
            "companyName": textEdittingControllers[0].text,
            "taxpayerIdNum": textEdittingControllers[1].text,
            "registerAddr": textEdittingControllers[2].text,
            "registerPhone": textEdittingControllers[3].text,
            "branchName": textEdittingControllers[4].text,
            "cardNo": textEdittingControllers[5].text,
            "receiverName": textEdittingControllers[6].text,
            "receiverPhone": textEdittingControllers[7].text,
            "receiverAddr": textEdittingControllers[8].text,
            "uid" : userService.userInfo.uid
          },
          onSuccess: (r, _) {
            GCInvoiceController i_controller = Get.find();
            i_controller.requestInvoiceInfo();
            Get.back();
          },
          onFailed: (err) {
            EasyLoading.showError(err);
          }
      );
    }
  }

  clearTap() {
    for (var value in textEdittingControllers) {
      value.text = "";
    }
  }


  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();

    if(Get.arguments[0] == "edit") {
      isEdit.value = true;
      InvoiceData invoiceData = Get.arguments[1];
      textEdittingControllers[0].text = invoiceData.companyName;
      textEdittingControllers[1].text = invoiceData.taxpayerIdNum;
      textEdittingControllers[2].text = invoiceData.registerAddr;
      textEdittingControllers[3].text = invoiceData.registerPhone;
      textEdittingControllers[4].text = invoiceData.branchName;
      textEdittingControllers[5].text = invoiceData.cardNo;
      textEdittingControllers[6].text = invoiceData.receiverName;
      textEdittingControllers[7].text = invoiceData.receiverPhone;
      textEdittingControllers[8].text = invoiceData.receiverAddr;

    } else {
      isEdit.value = false;
    }
  }

}